﻿using DLLPC.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VisionCore
{
    public class Status_ExcInit : ProcedureHelper_Exc
    {
        string subStatusCode = "";
        string tempStatusCode = "";
        bool condition = false;
        bool start = false;

        SystemTimeCounter scStatus = new SystemTimeCounter();
        public void Main()
        {
            condition = !ExcHomeComp
                       && Kernel.motion.CV1_Driver.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.CV1_Y.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.CV1_TrackSW_Y.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.CV2_Driver.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.CV2_Y.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.CV2_TrackSW_Y.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.CCD1_X.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.CCD1_Y.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.CCD1_Z.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.CCD2_X.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.CCD2_Y.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.CCD2_Z.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.Insp1_CV1.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.Insp1_CV2.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.Insp1_Y.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.Insp2_CV1.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.Insp2_CV2.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.Insp2_Y.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.Insp3_CV1.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.Insp3_CV2.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.Insp3_Y.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.Insp4_CV1.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.Insp4_CV2.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP
                       && Kernel.motion.Insp4_Y.axis.State != Advantech.Motion.AxisState.STA_AX_ERROR_STOP;
            start = condition && InitMode;

            switch (ExcInitStatusCode)
            {
                case ExcInitStatus.Init:
                    subStatusCode = "00: 准备";
                    {
                        if (start)
                        {
                            ExcInitStatusCode = ExcInitStatus.Cylinder_ToDown;
                        }
                    }
                    break;
                case ExcInitStatus.Cylinder_ToDown:
                    subStatusCode = "05: 倒膜气缸上升及载台气缸下降";
                    {
                        if (CV1_Cyl_Dn && CV2_Cyl_Dn && Insp1_Cyl_Dn && Insp2_Cyl_Dn && Insp3_Cyl_Dn && Insp4_Cyl_Dn)
                        {
                            ExcInitStatusCode = ExcInitStatus.VacConfirm;
                        }
                    }
                    break;
                case ExcInitStatus.VacConfirm:
                    subStatusCode = "06:真空确认";
                    {
                        if ( ((  ParameterHelper.Insp1.Enable && Insp1_ToVacOnn) || (!ParameterHelper.Insp1.Enable && !Insp1_ToVacOnn))
                            && ((ParameterHelper.Insp2.Enable && Insp2_ToVacOnn) || (!ParameterHelper.Insp2.Enable && !Insp2_ToVacOnn))
                            && ((ParameterHelper.Insp3.Enable && Insp3_ToVacOnn) || (!ParameterHelper.Insp3.Enable && !Insp3_ToVacOnn))
                            && ((ParameterHelper.Insp4.Enable && Insp4_ToVacOnn) || (!ParameterHelper.Insp4.Enable && !Insp4_ToVacOnn)))
                        {
                            ExcInitStatusCode = ExcInitStatus.ToHome_1fst;
                        }
                    }
                    break;
            
                case ExcInitStatus.ToHome_1fst:
                    subStatusCode = "7: CCD_Z轴回原点";
                    {
                        if (CCD1_Z_Homed && CCD2_Z_Homed )
                        {
                            ExcInitStatusCode = ExcInitStatus.CCD_ZToWait;
                        }                        
                    }
                    break;
                case ExcInitStatus.CCD_ZToWait:
                    subStatusCode = "8: CCD_Z轴去待命位";
                    {
                        if (CCD1_Z_Wait&& CCD2_Z_Wait)
                        {
                            ExcInitStatusCode = ExcInitStatus.ToHome_2nd;
                        }
                    }

                    break;
                case ExcInitStatus.ToHome_2nd:
                    subStatusCode = "10: 所有轴回原点";
                    {
                        if (CV1_Y_Homed && CV1_TrackSW_Y_Homed && CV2_Y_Homed && CV2_TrackSW_Y_Homed && Insp1_Y_Homed && Insp2_Y_Homed && Insp3_Y_Homed && Insp4_Y_Homed&&CCD1_X_Homed && CCD1_Y_Homed&& CCD2_X_Homed && CCD2_Y_Homed)
                        {
                            ExcInitStatusCode = ExcInitStatus.HomeComplete;
                        }
                    }

                    break;
                case ExcInitStatus.InspST_ToWork:
                    subStatusCode = "20: 所有轴去工作位";
                    {
                        if (ExcTR_R_LD)
                        {
                            ExcInitStatusCode = ExcInitStatus.HomeComplete;
                        }
                    }

                    break;


                case ExcInitStatus.HomeComplete:
                    subStatusCode = "18: 原点完成";
                    {
                        ExcInitStatusCode = ExcInitStatus.Init;
                    }
                    break;
                default:
                    break;
            }

            LogStatusCode(subStatusCode);
        }

        private void LogStatusCode(string msg)
        {
            if (msg != tempStatusCode)
            {
                tempStatusCode = msg;
                LogMsg("主体回原点 " + tempStatusCode);
            }
        }
    }
}
